package com.mbc.common.manager;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;

import com.mbc.common.CommonSingleton;
import com.mbc.common.dao.BranchTypeDAO;
import com.mbc.common.dao.CommonDAOFactory;
import com.mbc.common.dao.LogDAO;
import com.mbc.common.entities.BranchType;
import com.mbc.common.entities.Log;
import com.mbc.common.exception.DataDuplicationException;
import com.mbc.common.utils.KeyGenerator;
import com.mbc.common.data.BranchTypeDataList;
import com.mbc.common.entities.BranchTypeRpt;

public class BranchTypeMgr extends AbstractMgr {
	
	private BranchTypeDAO branchTypeDAO;
	private LogDAO logDAO;
	
	public BranchTypeMgr(){}
	
	public BranchTypeMgr(String sessionId){
		this.sessionId = sessionId;
	}
	public void create(BranchType entity) throws Exception {
		openConnection();
		try {
			branchTypeDAO = CommonDAOFactory.getBranchTypeDAO(dbManager.getConnection());
			entity.setBranchTypeId(KeyGenerator.generateKey(getConnection()));
			branchTypeDAO.create(entity);
			
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			Log l_logentity = new Log();
			l_logentity.setTablename("BranchType");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(entity.getBranchTypeId());
			l_logentity.setUserid(entity.getUserid());
			l_logentity.setLogstatus(Log.__SAVE_DATA);
			logDAO.getData(l_logentity);
			
		} catch (SQLException e) {
			if(DataDuplicationException.UNIQUE_KEY_ERROR_CODE == e.getErrorCode())
				throw new DataDuplicationException(e.getMessage());
			else
				throw e;
		} finally {
			closeConnection();
		}
	}
	public void update(BranchType entity) throws Exception {
		openConnection();
		try {
			branchTypeDAO = CommonDAOFactory.getBranchTypeDAO(dbManager.getConnection());
			branchTypeDAO.update(entity);
			
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			Log l_logentity = new Log();
			l_logentity.setTablename("BranchType");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(entity.getBranchTypeId());
			l_logentity.setUserid(entity.getUserid());
			l_logentity.setLogstatus(Log.__UPDATE_DATA);
			logDAO.getData(l_logentity);
			
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	public void delete(long key,long userid) throws Exception {
		openConnection();
		try {
			branchTypeDAO = CommonDAOFactory.getBranchTypeDAO(dbManager.getConnection());
			
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			Log l_logentity = new Log();
			l_logentity.setTablename("BranchType");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(key);
			l_logentity.setUserid(userid);
			l_logentity.setLogstatus(Log.__DELETE_DATA);
			logDAO.getData(l_logentity);
			
			branchTypeDAO.delete(key);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	public BranchType find(long key) throws Exception {
		openConnection();
		try {
			branchTypeDAO = CommonDAOFactory.getBranchTypeDAO(dbManager.getConnection());
			return branchTypeDAO.find(key);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	public ArrayList<BranchType> findAll()throws Exception{
		openConnection();
		try {
			branchTypeDAO = CommonDAOFactory.getBranchTypeDAO(dbManager.getConnection());
			return branchTypeDAO.findAll();
		} catch (Exception e) {
			throw e;
		} finally {
			dbManager.closeConnection();
		}
	}
	public BranchTypeDataList findBranchTypes(BranchType cri)throws Exception{
		openConnection();
		try {
			branchTypeDAO = CommonDAOFactory.getBranchTypeDAO(dbManager.getConnection());
			return branchTypeDAO.findBranchTypes(cri);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public JasperPrint prepareBranchTypeReport(BranchTypeRpt entity) throws Exception {
		openConnection();
		JasperPrint l_jsPrint = null;
		try {
			branchTypeDAO = CommonDAOFactory.getBranchTypeDAO(getConnection());

			JRResultSetDataSource l_jrRS = new JRResultSetDataSource(branchTypeDAO.prepareBranchTypeReport(entity));
			// String l_path = ServerGlobal.getAppPath();

			 JasperReport l_jsRpt =(JasperReport)JRLoader.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH +"reports/BranchType.jasper");
			// l_params.put("Cri",info.getCri());
			 HashMap<String, Object> l_params = new HashMap<String, Object>();
				l_params.put("companyname", "mbc");
			l_jsPrint = JasperFillManager.fillReport(l_jsRpt, l_params, l_jrRS);

		} catch (JRException je) {
			throw je;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}

		return l_jsPrint;

	}
	
	public BranchType findBranchTypeByCode(BranchType entity)throws Exception{
		openConnection();
		try {
			branchTypeDAO = CommonDAOFactory.getBranchTypeDAO(dbManager.getConnection());
			return branchTypeDAO.findBranchTypeByCode(entity);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
}
